<?php
Yii::import("application.modules.manage.util.PageBar");
class LanguageController extends ManageController {

    public function actionIndex($current=1) {
        $langs = $this->connection->createCommand("select * from xm_lang order by id asc")->queryAll();
        $this->render ('index',array(
            'langs'=>$langs
        ));
    }

    public function actionShowedit($id){
        $lang =  $this->connection->createCommand(" select * from xm_lang where id = {$id} ")->queryRow();

        $messages = $this->connection->createCommand("select sm.*,m.* from sourcemessage sm LEFT JOIN message m on sm.id = m.id where m.`language` = '".$lang['en']."'")->queryAll();

        $this->render("showedit",Array('lang'=>$lang,'messages'=>$messages));
    }

    public function actionEdit(){
        $langid = $_REQUEST['langid'];
        $lang =  $this->connection->createCommand(" select * from xm_lang where id = {$langid} ")->queryRow();

        $messageids = $this->connection->createCommand("select m.id from sourcemessage sm LEFT JOIN message m on sm.id = m.id where m.`language` = '".$lang['en']."'")->queryAll();
        foreach($messageids as $m){
            $tmsg = $_REQUEST['transl_'.$m['id']];
            $this->connection->createCommand("update message set translation = '{$tmsg}' where language = '".$lang['en']."' and id = {$m['id']}  ")->query();
        }
        $this->redirect("showedit?id={$langid}");
    }

}